﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace ReflectorHelper
{
    public class FileHelper
    {
        public static ArrayList ReadTxtFile(string path, string fgf)
        {
            if (!File.Exists(path))
            {
                return null;
            }
            try
            {
                //读出一行文本，并临时存放在ArrayList中
                StreamReader sr = new StreamReader(path, Encoding.Default);
                string l;
                ArrayList content = new ArrayList();
                while ((l = sr.ReadLine()) != null)
                {
                    ArrayList row = new ArrayList();
                    int length = l.Length;
                    int i = 0;
                    bool fgf_flag = false;
                    while (i < length)
                    {
                        string x = l[i].ToString();
                        i++;
                        if (x.Equals(fgf))
                        {
                            fgf_flag = true;
                            row.Add(l.Substring(0, i - 1));
                            length = length - i + 1;
                            i = 0;
                        }
                    }
                    if (!fgf_flag)
                    {
                        if (l.Trim().StartsWith("using") && l.Trim().EndsWith(";"))
                        {
                            l = l.Trim();
                        }
                        l = l.Replace("CS$0$0001", "DonYang88").Replace("CS$1$0000", "DonYang888");
                        row.Add(l);
                    }
                    row.TrimToSize();

                    //将多行文本储存在ArrayList中，并返回；
                    if (l.Trim().StartsWith("using") && l.Trim().EndsWith(";"))
                    {
                        content.Insert(0, row);
                    }
                    else
                    {
                        content.Add(row);
                    }
                }
                sr.Close();
                content.TrimToSize();
                return content;
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
        //写文件；
        public static void WriteTxtFile(string path, string fgf, ArrayList a)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
                {
                    IEnumerator cIE = a.GetEnumerator();
                    while (cIE.MoveNext())
                    {
                        ArrayList row = (ArrayList)cIE.Current;

                        IEnumerator rIE = row.GetEnumerator();
                        int length = row.Count;
                        int i = 0;
                        while (rIE.MoveNext())
                        {
                            string st = rIE.Current.ToString();
                            if (i == length - 1)
                                sw.Write(st);
                            else
                                sw.Write(st + fgf);
                            i++;
                        }
                        sw.WriteLine();
                    }
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
    }
}
